package com.yc.config;

import com.yc.documentLoanding.ElasticsearchProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
//import org.elasticsearch.client.RestClient;
import javax.net.ssl.SSLContext;

@Configuration
public class ESConfig {

//    @Bean
//    public RestClient elasticsearchRestClient(ElasticsearchProperties props) throws Exception {
//        System.out.println(props);
//        // 1. 凭据提供者
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(
//                AuthScope.ANY,
//                new UsernamePasswordCredentials(props.getUsername(), props.getPassword())
//        );
//
//        // 2. SSL 上下文（信任所有证书 —— 仅开发用！）
//        SSLContext sslContext;
//        if (props.isUseHttps()) {
//            sslContext = SSLContextBuilder.create()
//                    .loadTrustMaterial((TrustStrategy) (chain, authType) -> true) // 信任所有
//                    .build();
//        } else {
//            sslContext = null;
//        }
//
//        // 3. 协议
//        String scheme = props.isUseHttps() ? "https" : "http";
//
//        // 4. 构建 RestClient
//        return RestClient.builder(new HttpHost(props.getHost(), props.getPort(), scheme))
//                .setHttpClientConfigCallback(httpClientBuilder -> {
//                    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                    if (sslContext != null) {
//                        httpClientBuilder
//                                .setSSLContext(sslContext)
//                                .setSSLHostnameVerifier((hostname, session) -> true); // 忽略主机名验证
//                    }
//                    return httpClientBuilder;
//                })
//                .build();
//    }
}
